package dk.muldbjergskolen.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.JTextField;

import dk.muldbjergskolen.ctr.FagCtr;

import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;

public class OpretFag extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtFag;
	private JTextField txtForkortelse;

	/**
	 * Create the dialog.
	 */
	public OpretFag(OpretFagListener listener) {
		
		opretFagListener = listener;
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Opret fag");
		setBounds(100, 100, 230, 152);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JTextPane txtpnBlah = new JTextPane();
		txtpnBlah.setEditable(false);
		txtpnBlah.setBackground(UIManager.getColor("Button.background"));
		txtpnBlah.setText("Fag:");
		txtpnBlah.setBounds(10, 11, 57, 20);
		contentPanel.add(txtpnBlah);
		{
			txtpnKortform = new JTextPane();
			txtpnKortform.setText("Forkortelse:");
			txtpnKortform.setBackground(SystemColor.menu);
			txtpnKortform.setBounds(10, 42, 57, 20);
			contentPanel.add(txtpnKortform);
		}
		{
			txtFag = new JTextField();
			txtFag.setBounds(77, 11, 127, 20);
			contentPanel.add(txtFag);
			txtFag.setColumns(10);
		}
		
		JTextPane textPane = new JTextPane();
		textPane.setText("Fag:");
		textPane.setEditable(false);
		textPane.setBackground(SystemColor.menu);
		textPane.setBounds(10, 42, 28, 20);
		contentPanel.add(textPane);
		
		txtForkortelse = new JTextField();
		txtForkortelse.setColumns(10);
		txtForkortelse.setBounds(77, 42, 127, 20);
		contentPanel.add(txtForkortelse);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
						opret();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{txtFag, txtForkortelse, okButton, cancelButton}));
	}
		
		//
		// Event håndtering.
		//

		// "Lyttere"
		OpretFagListener opretFagListener;
		private JTextPane txtpnKortform;
		private JButton okButton;
		private JButton cancelButton;

		public interface OpretFagListener
		{
			void OnNewFag();
		}

		/***
		 * Kaldes når en nyt fag er lavet.
		 */
		private void OnNewFag()
		{
			if (opretFagListener != null)
				opretFagListener.OnNewFag();
		}
		
		
		// opretter et fag
		public void opret()
		{
			FagCtr fCtr = new FagCtr();
			
			try
			{
			String navn = txtFag.getText();
			String forkortelse  = txtForkortelse.getText();
			
			fCtr.opretFag(navn, forkortelse);
			}
			catch (Exception e)
			{
				JOptionPane.showMessageDialog(txtFag, "Navn skal udfyldes");
				JOptionPane.showMessageDialog(txtForkortelse, "Forkortelse skal udfyldes");
				return;
			}
			OnNewFag();
			setVisible(false);
		}
		
		
		
		
}
	

